{include file="common/header" /}

<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">标题</label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" value="{:input('param.title')}" autocomplete="off" class="layui-input" >
                            </div>
                        </div>

                        <!-- <div class="layui-inline">
                            <label class="layui-form-label">所属栏目</label>
                            <div class="layui-input-inline">
                                <select name="cate_id">
                                  <option value="">请选择</option>
                                  {volist name="$treeList" id="vo"}
                                  <option value="{$vo.id}" {if $vo.id == $cate_id /} selected="" {/if}  {if $vo.module != $module /} disabled {/if} >{$vo.catname|RAW}</option>
                                  {/volist}
                                </select>
                            </div>
                        </div> -->
                        
                        <div class="layui-inline">
                            <button type="button" class="layui-btn layui-btn-primary" lay-submit  lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <input type="hidden" id="cate_id" value="{$cate_id}">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" onclick="add()" >添加</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

        <script type="text/html" id="is_open">
            <input type="checkbox" name="is_open" data-table="banner" data-id_name="id" data-id="{{d.id}}" data-field="is_open" lay-skin="switch" lay-text="是|否" lay-filter="switchIsOpen" {{ d.is_open == 1 ? 'checked' : '' }}>
        </script>

    </div>
</div>

<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layuimini = layui.layuimini;

        table.render({
            elem: '#currentTableId',
            url: "{:url(CONTROLLER_NAME.'/list')}?cate_id={:input('param.cate_id')}",
            toolbar: '#toolbarDemo',
            defaultToolbar: [],
            /*defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],*/
            cols: [[
                // {type: "checkbox", width: 50, fixed: "left"},
                {field: 'id', width: 80, title: 'ID', sort: true},
                
                {field: 'title',  title: '标题'},
                // {field: 'cates_name', width:150,  title: '所属栏目'},
                {field: 'img_url',width: 80, title: '图片',align:'center',templet:function(res){
                    var html = '';
                    
                    if(res.img_url){
                        html += '<a href="'+res.img_url+'" target="_blank" ><img style="width:50px;height:50px;" src="'+res.img_url+'"></a>';
                    }else{
                        html += '<img style="width:50px;height:50px;" src="__STATIC__/img/default.png">';
                    }
                    
                    return html;
                }},

                {field: 'is_open', width: 100, title: '是否开启', templet: '#is_open'},
                {field: 'orderid', align:'center',width: 100, title: '排序',edit: 'text', sort: true},
                {field: 'add_time', width: 135, title: '添加时间', sort: true},
                {title: '操作', width: 150, templet: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true
        });

        //是否开启
        form.on('switch(switchIsOpen)', function(data){
            var checked = data.elem.checked;
            //var role_id = data.elem.value;
            let table = $(data.elem).attr("data-table");
            let id_name = $(data.elem).attr("data-id_name");
            let id = $(data.elem).attr("data-id");
            let field = $(data.elem).attr("data-field");
            console.log(table)

            // var id = data.elem.attributes['switch_id'].nodeValue;
            // var pri_id = data.elem.attributes['switch_pri_id'].nodeValue;
            // var tempId = data.elem.attributes['switch_tempId'].nodeValue;

            $.ajax({
                url:"{:url(CONTROLLER_NAME.'/changeStatus')}",
                data: {'table':table,'checked':checked,'id_name':id_name,'id':id,'field':field},
                type:'post',
                dataType:'json',
                success:function(res){
                    // console.log(res);
                    // return false;
                    if(res.errcode == 1){
                        layer.msg(res.message,{icon: 1,time: 1000},function(){
                            // location.reload();
                        })
                    }else {
                        layer.msg(res.message,{icon: 2,time: 2000})
                    }
                    // parent.layer.close(index);
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('网络失败，请刷新页面后重试!',{icon: 2,time: 2000})
                }
            });
           
            
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //var result = JSON.stringify(data.field);
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });
            console.log(data.field)
            table.reload('currentTableId',{
                where: data.field
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            })
            $("#cate_id").val(data.field.cate_id);
            return false;
        });

        window.add=function(){
            let cate_id = $("#cate_id").val();
            window.location.href="{:url(CONTROLLER_NAME.'/add')}?cate_id="+cate_id;
        };

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                let cate_id = $("#cate_id").val();
                window.location.href="{:url(CONTROLLER_NAME.'/edit')}?id="+data.id+"&cate_id="+cate_id;
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确认删除吗', function (index) {
                    
                    $.ajax({
                        url:"{:url(CONTROLLER_NAME.'/delete')}",
                        data: {id:data.id,'table':"{:CONTROLLER_NAME}",'id_name':'id'},
                        type:'post',
                        dataType:'json',
                        success:function(res){
                            // console.log(res);
                            // return false
                            if(res.errcode == 1){
                                layer.msg(res.message,{icon: 1,time: 1000},function(){
                                    obj.del(); //删除对应行（tr）的DOM结构
                                    // location.reload();
                                })
                            }else {
                                layer.msg(res.message,{icon: 2,time: 2000})
                            }
                            // parent.layer.close(index);
                        },
                        error : function(XMLHttpRequest, textStatus, errorThrown) {
                            layer.msg('网络失败，请刷新页面后重试!',{icon: 2,time: 2000})
                        }
                    });

                });
            }
        });

    });
</script>

</body>
</html>